<template>
  <el-container>
    <!-- 头部 -->
    <el-header>
      <div>
        <img src="../assets/images/logo(2).png" alt="" />
        <span>电商后台管理</span>
      </div>
      <el-button type="info" @click="loginout">退出</el-button>
    </el-header>
    <!-- 主体 -->
    <el-container>
      <!-- 侧边栏 -->
      <el-aside :width="width" v-model="isflat">
        <div class="toggle-button" @click="toggleAsideWidth">
          <i class="el-icon-s-operation"></i>
        </div>
        <el-menu
          text-color="#fff"
          active-text-color="#E65812"
          background-color="rgba(0,0,0,.3)"
          unique-opened
          :collapse="isflat"
          :collapse-transition="false"
          :router="true"
          :default-active="activePath"
        >
          <!-- 一级菜单-->
          <el-submenu
            submenuindex="1"
            v-for="(item, i) in menuList"
            :key="item.id"
            :index="item.id + ''"
          >
            <!-- 一级菜单的模板区域 -->
            <template slot="title">
              <!-- 图标 -->
              <i :class="icon[i]"></i>
              <!-- 文本 -->
              <span>{{ item.authName }}</span>
            </template>

            <!-- 二级菜单-->
            <el-menu-item
              v-for="childitem in item.children"
              :index="'/' + childitem.path"
              :key="childitem.id"
              @click="
                saveState(
                  '/' + childitem.path,
                  item.authName,
                  childitem.authName
                )
              "
              :sparechild="sparemevalue"
            >
              <!-- 二级菜单的模板区域 -->
              <template slot="title">
                <!-- 图标 -->
                <i class="el-icon-menu"></i>
                <!-- 文本 -->
                <span>{{ childitem.authName }}</span>
              </template>
            </el-menu-item>
            <!--  -->
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 内容 -->
      <el-main>
        <!-- 放一个路由占位符 -->
        <Breadcrumb :child="sparemevalue"></Breadcrumb>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
import Breadcrumb from '../components/Breadcrumb'
export default {
  created() {
    this.getMenuList()
    this.activePath = window.sessionStorage.getItem('activepath')
  },
  data() {
    return {
      // 传值给子组件 面包屑导航
      sparemevalue: {
        firBrea: '',
        secnBrear: ''
      },
      menuList: [],
      icon: [
        'el-icon-s-custom',
        'el-icon-s-promotion',
        'el-icon-goods',
        'el-icon-tickets',
        'el-icon-s-marketing'
      ],
      width: '200px',
      // 是否折叠
      isflat: false,
      // 当前状态
      activePath: ''
    }
  },
  components: {
    Breadcrumb
  },
  methods: {
    // 退出
    loginout() {
      window.sessionStorage.removeItem('token')
      this.$router.push('/login')
    },
    // 获取左侧菜单栏
    getMenuList() {
      // 利用钩子函数，获取左侧菜单栏
      this.$ajax.get('/menus').then(res => {
        // 进行判断是否ok
        if (res.status !== 200) return this.$message.error('失败')
        // console.log(res.data.data)
        this.menuList = res.data.data
      })
    },
    // 变换左侧菜单栏
    toggleAsideWidth() {
      if (!this.isflat) {
        this.isflat = !this.isflat
        this.width = '64px'
      } else {
        this.isflat = !this.isflat
        this.width = '200px'
      }
    },
    // 保存状态
    saveState(activepath, onebanxie, twobanxie) {
      window.sessionStorage.setItem('activepath', activepath)
      // 对状态赋值，进行动态切换
      this.activePath = activepath
      this.sparemevalue.firBrea = onebanxie
      this.sparemevalue.secnBrear = twobanxie
    }
  }
}
</script>
<style lang="less" scoped>
.el-container {
  height: 100%;
}
.el-menu {
  border: none;
}
.el-header {
  // background-color: #fd811e;
  background-image: url('../assets/images/bg.jpg');
  background-size: cover;
  display: flex;
  justify-content: space-between;
  // 竖直放向上
  align-items: center;
  padding: 10px 0;
  padding-right: 30px;
  padding-left: 5px;
  img {
    height: 50px;
    border-radius: 50%;
    vertical-align: middle;
  }
  span {
    color: #fff;
    font-weight: 500;
    font-size: 22px;
    margin-left: 20px;
  }
}
.el-aside {
  // background-color: rgb(236, 171, 182);

  background-image: url('../assets/images/bg.jpg');
  background-size: cover;
}
.toggle-button {
  padding: 5px 0;
  text-align: center;
  background-color: rgba(0, 0, 0, 0.6);
  color: #fff;
  cursor: pointer;
}
.el-main {
  background-color: #eaedf1;
}
</style>
